package com.newbee.activity;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.gauss.render.SpeexPlayer;
import com.hhu.newbee.R;
import com.newbee.adapter.VoiceFileListvewAdapter;
import com.newbee.listbean.VoiceFileBean;
import com.newbee.progressdlg.PlayProgressDlg;
import com.newbee.sound.Sound;
import com.newbee.sqlite.DBHelper;

public class VoiceFileListActivity extends Activity implements
		OnMenuItemClickListener, OnClickListener, OnItemClickListener
{

	private SwipeMenuListView mListView;

	private DBHelper mDbHelper;

	private VoiceFileListvewAdapter fla;
	private List<VoiceFileBean> mDataList;
	private Builder builder;

	private Builder palyBuilder;

	private SpeexPlayer splayer = null;

	private Handler handler;
	private PlayProgressDlg dlg;

	private int MSG_PALY_DLG = 1;

	private View mEmptyView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_voice_recg_list);
		mDbHelper = new DBHelper(this);
		initLayout();
		mEmptyView = this.findViewById(R.id.msglist_empty);
		initListView();

		builder = new Builder(this);
		mListView.setOnMenuItemClickListener(this);
		mListView.setOnItemClickListener(this);
		dlg = PlayProgressDlg.createDialog(this);
		dlg.setOnCancelListener(new OnCancelListener()
		{

			@Override
			public void onCancel(DialogInterface arg0)
			{
				splayer.stopPlay();
				Toast.makeText(VoiceFileListActivity.this, "已停止播放",
						Toast.LENGTH_SHORT).show();

			}
		});
		dlg.setCancelable(true);
		dlg.setMessage("正在播放录音 ");
		handler = new Handler()
		{

			@Override
			public void handleMessage(Message msg)
			{
				if (msg.what == MSG_PALY_DLG)
				{
					dlg.hide();
				}
			}

		};
		SwipeMenuCreator creator = new SwipeMenuCreator()
		{

			@Override
			public void create(SwipeMenu menu)
			{
				// create "open" item
				SwipeMenuItem openItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				openItem.setBackground(VoiceFileListActivity.this
						.getResources().getDrawable(R.color.steelblue));
				// set item width
				openItem.setWidth(dp2px(70));
				// set item title
				openItem.setTitle("转换");
				// set item title fontsize
				openItem.setTitleSize(18);
				// set item title font color
				openItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(openItem);

				// create "paly" item
				SwipeMenuItem playItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				playItem.setBackground(VoiceFileListActivity.this
						.getResources().getDrawable(R.color.darkgreen));
				// set item width
				playItem.setWidth(dp2px(70));
				// set a icon
				// set item title
				playItem.setTitle("播放");
				// set item title fontsize
				playItem.setTitleSize(18);
				// set item title font color
				playItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(playItem);

				// create "delete" item
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				deleteItem.setBackground(VoiceFileListActivity.this
						.getResources().getDrawable(R.color.red));
				// set item width
				deleteItem.setWidth(dp2px(70));
				// set a icon
				// set item title
				deleteItem.setTitle("删除");
				// set item title fontsize
				deleteItem.setTitleSize(18);
				// set item title font color
				deleteItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(deleteItem);
			}
		};
		mListView.setMenuCreator(creator);
		mListView.setOnMenuItemClickListener(this);

	}

	private void initDialog()
	{

		this.palyBuilder = new Builder(this);

	}

	private void initListView()
	{

		mDataList = mDbHelper.getVoiceFileList();

		if (mDataList.size() == 0)
		{
			mListView.setEmptyView(mEmptyView);
		}
		else
		{
			fla = new VoiceFileListvewAdapter(mDataList, this);
			mListView.setAdapter(fla);
		}

	}

	private void deleteListViewItem(int position, int id)
	{

		mDbHelper.deleteVoiceFile(id);
		mDataList.remove(position);
		fla.notifyDataSetChanged();
		if (mDataList.size() == 0)
		{
			mListView.setEmptyView(mEmptyView);
		}

	}

	private void initLayout()
	{

		this.findViewById(R.id.btn_back).setOnClickListener(this);
		mListView = (SwipeMenuListView) this
				.findViewById(R.id.voice_recg_listview);
	}

	@Override
	protected void onDestroy()
	{

		mDbHelper.close();
		super.onDestroy();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{

		case R.id.btn_back:

			this.finish();
			break;

		default:
			break;
		}

	}

	private int dp2px(int dp)
	{
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}

	@Override
	public boolean onMenuItemClick(final int position, SwipeMenu menu, int index)
	{

		final VoiceFileBean bean = mDataList.get(position);
		switch (index)
		{
		case 0:
		{

			Intent intent = new Intent(this, VoiceAfterTransActivity.class);
			intent.putExtra(VoiceAfterTransActivity.VOICE_FILE_NAME,
					bean.getVfname());

			intent.putExtra(VoiceAfterTransActivity.DATE, bean.getVftime());

			intent.putExtra(VoiceAfterTransActivity.TYPE, bean.getVtppe());

			this.startActivity(intent);
			break;
		}
		case 1:
		{
			splayer = new SpeexPlayer(Sound.DIR + bean.getVfname());
			splayer.startPlay();

			dlg.show();

			String[] hms = bean.getVlength().split(":");
			int h = Integer.parseInt(hms[0]);
			int m = Integer.parseInt(hms[1]);
			int s = Integer.parseInt(hms[2]);
			int sum = (h * 60 + m) * 60 + s;
			Message msg = new Message();
			msg.what = MSG_PALY_DLG;
			handler.sendMessageDelayed(msg, sum * 1000);
			break;
		}
		case 2:
		{
			builder.setTitle("确认");
			builder.setMessage("你确认要删除吗？");
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface arg0, int arg1)
						{

							deleteListViewItem(position, bean.getVid());
						}
					}).setNegativeButton("取消",
					new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface arg0, int arg1)
						{

						}
					});
			builder.create().show();
			break;
		}

		}
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	{
		// TODO Auto-generated method stub

	}
}
